import * as API_Members from '../../api/members'
import * as API_Distribution from '../../api/distribution'
import { seller_id,shareMessage } from "../../config/config";
Page({
  data: {
    // webSrc: 'http://127.0.0.1:3008/explosive/home'
    webSrc: 'https://h5.oupinguoji.cn/explosive/home',
    // webSrc: 'http://192.168.0.47:3008/explosive/home' // 本地
    query: "",
    sharename:'',
    shareimg:'',
  },
  onLoad(options){
    API_Distribution.getsharemessage(seller_id).then( res => {
      // console.log( res) 
      this.setData({
        sharename:res.shop_mini_title,
        shareimg:res.shop_mini_share
      })
    }).catch( err => {
      console.error( err)
    })
	if (!wx.getStorageSync('refresh_token')) { // 未登录
		
	} else {
		API_Members.getUserInfo().then((response) => {
		  this.setData({
			webSrc: 'https://h5.oupinguoji.cn/explosive/home?points=' + response.consum_point
			// webSrc: 'http://192.168.0.47:3008/explosive/home?points=' + response.consum_point 
		  })
		})
  }
           // 上下级关系获取绑定
           if(options.su){
            API_Distribution.accessShortLink({ su: options.su }).then(res => {
              wx.setStorageSync('inviteCode',res.mobile)
            })
          }
  },
  //刷新webview页面
  async onShow(){
    // let tmpUrl = this.data.webSrc;
    // this.setData({ webSrc: ''});
    // setTimeout(() => {this.setData({webSrc: tmpUrl})}, 10);
    if (wx.getStorageSync("access_token")){
    let response = await API_Distribution.generateShortLink();
    let query = response.message.substring(
      response.message.indexOf("?"),
      response.message.length
    );
    this.setData({
      query
    })
  }
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    wx.showShareMenu({
      withShareTicket: true,
    });
    return {
      title: this.data.sharename,
      desc: this.data.sharename,
      path: '/pages/home/home' + this.data.query,
      imageUrl: this.data.shareimg
    }
  }
})
